<?php /*0*/

/**
  *	This file contains a class
  *
  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
  *	@package CMS
  *	@version 0.0
  */

if ( !defined( 'SECURITY' ) ) {
	die( 'Direct access to this script is not allowed!' );
}

/**
  *	This class is responsible for operations with classes
  *
  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
  *	@version 0.0
  */
class SimpleDirectory {

	/**
	  *	This method creates directories recursively
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string wanted address <br />
	  *
	  *	@return bool true on success
	  */
	public static function createDirectories( $arguments ) {
		
		$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) );
		
		if ( !$address || !is_string( $address ) ) {
			return false;
		}
		
		$address = SimpleFile::getFullFileAddress( array( 'address' => $address ) );
		
		$path_array = explode( '/', $address );
		
		$temp_address = '';
		
		$path_array_id = 0;
		
		$was_empty = false;
		
		while( $temp_address != $address ) {
			
			if ( empty( $temp_address ) && !$was_empty ) {
				$was_empty = true;
				$temp_address .= $path_array[$path_array_id++];
			} else {
				$temp_address .= '/' . $path_array[$path_array_id++];
			}
			
			if ( !empty( $temp_address ) && !file_exists( $temp_address ) ) {
				if ( strlen( $temp_address ) > strlen( constant( 'ROOT_DIR' ) ) ) {
					if ( !mkdir( $temp_address, constant( 'DEFAULT_DIRECTORY_RIGHTS' ) ) ) {
						return false;
					}
				}
			}
			
		}
		
		return true;
		
	}
	
	/**
	  *	This method deletes a directory and everything inside it
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string directory address <br />
	  *
	  *	@return bool true on success
	  */
	public static function deleteDirectory( $arguments ) {
		
		$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) );
		
		if ( !$address || !is_string( $address ) ) {
			return false;
		}
		
		if ( !file_exists( $address ) ) {
			return false;
		}
		
		$files = SimpleFile::getFiles( array( 'address' => $address ) );
		
		if ( ( false === $files ) || empty( $files ) ) {
			if ( !rmdir( $address ) ) {
				return false;
			}
		} elseif ( is_array( $files ) ) {
			foreach( $files as $file ) {
				if ( is_dir( $address . '/' . $file ) ) {
					if ( false === SimpleDirectory::deleteDirectory( array( 'address' => $address . '/' . $file ) ) ) {
						return false;
					}
				} else {
					if ( false === SimpleFile::deleteFile( array( 'address' => $address . '/' . $file ) ) ) {
						return false;
					}
				}
			}
		} else {
			return false;
		}
		
		if ( file_exists( $address ) ) {
			if ( !@rmdir( $address ) ) {
				return false;
			}
		}
		
		return true;
		
	}

}

?>